package com.rms.hkpc.fragment;

import android.text.TextUtils;
import android.view.View;
import android.view.ViewStub;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.rms.hkpc.R;
import com.rms.hkpc.apiresult.ApiResultData;
import com.rms.hkpc.db.bean.Event;
import com.rms.hkpc.db.dto.ShortDto;
import com.rms.hkpc.constant.AllConstants;
import com.rms.hkpc.dialog.ResultDialog;
import com.rms.hkpc.dialog.ShortDialog;
import com.rms.hkpc.fragment.base.BaseFragment;
import com.rms.hkpc.presenter.FilterPresenter;
import com.rms.hkpc.util.DateUtil;
import com.rms.hkpc.view.IFilterView;

import org.greenrobot.eventbus.EventBus;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class FilterFragment extends BaseFragment<IFilterView, FilterPresenter> implements IFilterView {

    private EditText etDateStart, etDateEnd, etCode, etItem, etValue;
    private TextView tvResult, tvStatus;
    private int what, result = 2;
    private Calendar calendarStart, calendarEnd;
    private String statusId;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_filter;
    }

    @Override
    protected FilterPresenter createPresenter() {
        return new FilterPresenter();
    }

    @Override
    protected void initView() {
        OnClickListener onClickListener = new OnClickListener();
        if (getArguments() != null) {
            what = getArguments().getInt(AllConstants.WHAT);
            ViewStub vs = fd(R.id.vs_filter);
            try {
                vs.inflate();
                tvStatus = fd(R.id.tv_status);
                tvStatus.setOnClickListener(onClickListener);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        etDateStart = fd(R.id.et_date_start);
        etDateEnd = fd(R.id.et_date_end);
        etCode = fd(R.id.et_code);
        etItem = fd(R.id.et_item);
        etValue = fd(R.id.et_value);
        fd(R.id.tv_date_start).setOnClickListener(onClickListener);
        fd(R.id.iv_date_end).setOnClickListener(onClickListener);
        fd(R.id.btn_search).setOnClickListener(onClickListener);
        tvResult = fd(R.id.tv_result);
        tvResult.setOnClickListener(onClickListener);
        calendarStart = Calendar.getInstance();
        calendarEnd = Calendar.getInstance();
    }

    @Override
    public String getBat() {
        return etDateStart.getText().toString().replace("/", "-");
    }

    @Override
    public String getEat() {
        return etDateEnd.getText().toString().replace("/", "-");
    }

    @Override
    public String getCode() {
        return etCode.getText().toString();
    }

    @Override
    public String getItem() {
        return etItem.getText().toString();
    }

    @Override
    public String getValue() {
        return etValue.getText().toString();
    }

    @Override
    public Integer getResult() {
        return result;
    }

    @Override
    public String getStatusId() {
        return statusId;
    }

    @Override
    public void onGetStatusSuccess(ApiResultData<List<ShortDto>> apiResult) {
        if (apiResult.getStatus() != 2000) {
            Toast.makeText(context, apiResult.getMessage(), Toast.LENGTH_LONG).show();
            return;
        }
        new ShortDialog(context, apiResult.getData(), R.string.p_sel, statusId,
                new ShortDialog.IChoiceListener() {
                    @Override
                    public void getChoiceFromDialog(ShortDto bean) {
                        tvStatus.setText(bean.getName());
                        statusId = bean.getId();
                    }
                }).show();
    }

    @Override
    public void onGetStatusFails(Exception msg) {
        Toast.makeText(context, R.string.network_error, Toast.LENGTH_LONG).show();
    }

    class OnClickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.tv_date_start:
                    DateUtil.getInstance().showDateSelector(getActivity(), v,calendarStart, new DateUtil.ShowTimeSelectorCallback() {
                        @Override
                        public void OnTimeSelectListener(Date date) {
                            calendarStart.setTime(date);
                            etDateStart.setText(DateUtil.getInstance().DateTimeToStr(date));
                        }
                    });
                    break;
                case R.id.iv_date_end:
                    DateUtil.getInstance().showDateSelector(getActivity(),v, calendarEnd, new DateUtil.ShowTimeSelectorCallback() {
                        @Override
                        public void OnTimeSelectListener(Date date) {
                            calendarEnd.setTime(date);
                            etDateEnd.setText(DateUtil.getInstance().DateTimeToStr(date));
                        }
                    });
                    break;
                case R.id.tv_result:
                    new ResultDialog(context, result, new ResultDialog.IDoneListener() {
                        @Override
                        public void getDoneFromDialog(int str, int resultId) {
                            tvResult.setText(str);
                            result = resultId;
                        }
                    }).show();
                    break;
                case R.id.tv_status:
                    mPresenter.getStatus();
                    break;
                case R.id.btn_search:
                    if (!TextUtils.isEmpty(getBat()) && TextUtils.isEmpty(getEat())) {
                        Toast.makeText(context, R.string.end_time, Toast.LENGTH_LONG).show();
                        return;
                    }
                    if (TextUtils.isEmpty(getBat()) && !TextUtils.isEmpty(getEat())) {
                        Toast.makeText(context, R.string.start_time, Toast.LENGTH_LONG).show();
                        return;
                    }
                    // Record
                    if (what == 0) {
                        EventBus.getDefault().post(new Event(0, getBat(), getEat(), getCode(), getItem(), getValue(), getResult()));
                    } else { // WorksRecord
                        EventBus.getDefault().post(new Event(1, getBat(), getEat(), getCode(), getItem(), getValue(), getResult(), getStatusId()));
                    }
                    break;
            }
        }
    }

    @Override
    protected void initData() {

    }
}
